package com.example.firstjavamqtt.controller.base_api;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.firstjavamqtt.common.result.Result;
import com.example.firstjavamqtt.model.base.User;
import com.example.firstjavamqtt.service.base.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;

@RestController
@RequestMapping("/base/user")
@Api(tags = "用户接口")
public class UserController {

    @Autowired
    UserService userService;

    @PostMapping("/register")
    @ApiOperation("注册接口")
    public Result register(@RequestBody User user){
        userService.register(user);
        return Result.ok(user);
    }

    @PostMapping("/login")
    @ApiOperation("登录接口")
    public Result login(@RequestBody User user){
        userService.login(user);
        return Result.ok(user);
    }

    @ApiOperation("登出接口")
    @GetMapping("/logout")
    public void logout(HttpServletResponse response) {
        Subject subject= SecurityUtils.getSubject();
        subject.logout();
        response.setStatus(302);
    }

    @GetMapping("/getAll/{pageNum}/{limit}")
    @ApiOperation("分页查询所有用户")
    public Result login(@PathVariable int pageNum, @PathVariable int limit){
        Page<User> pageModel = userService.getAll(pageNum,limit);
        return Result.ok(pageModel);
    }
}
